import Vue from "vue";
import axios from "axios";
import Qs from "qs";
import { getStore, setStore, getSession, setSession } from "./storage";
import { router } from "../router/index";
import { Message } from "iview";
import Cookies from "js-cookie";
// 统一请求路径前缀
let base = "/xboot";
// 超时设定
axios.defaults.timeout = 150000;
axios.defaults.withCredentials = true;
axios.interceptors.request.use(
  (config) => {
    return config;
  },
  (err) => {
    Message.error("请求超时");
    return Promise.resolve(err);
  }
);

// http response 拦截器
axios.interceptors.response.use(
  (response) => {
    const data = response.data;
    // 根据返回的code值来做不同的处理(和后端约定)
    switch (data.code) {
      case 401:
        // 未登录 清除已登录状态
        Cookies.set("userInfo", "");
        setSession("accessToken", "");
        // router.push('/login');
        break;
      case 403:
        // 没有权限
        if (data.message !== null) {
          Message.error(data.message);
        } else {
          Message.error("未知错误");
        }
        break;
      case 500:
        // 错误
        if (data.message !== null) {
          Message.error(data.message);
        } else {
          Message.error("未知错误");
        }
        break;
      default:
        return data;
    }

    return data;
  },
  (err) => {
    // 返回状态码不为200时候的错误处理
    // Message.error(err.toString());
    return Promise.resolve(err);
  }
);

export const getRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "get",
    url: `${base}${url}`,
    params: params,
    headers: {
      accessToken: accessToken,
    },
  });
};
export const postRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${base}${url}`,
    data: params,
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      accessToken: accessToken,
    },
  });
};
export const postJsonRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${base}${url}`,
    data: params,
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      accessToken: accessToken,
    },
  });
};
export const putRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "put",
    url: `${base}${url}`,
    data: params,
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      accessToken: accessToken,
    },
  });
};

export const deleteRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "delete",
    url: `${base}${url}`,
    params: params,
    headers: {
      accessToken: accessToken,
    },
  });
};

export const uploadFileRequest = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${base}${url}`,
    params: params,
    headers: {
      accessToken: accessToken,
    },
  });
};

export const getAx = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "get",
    url: `${url}`,
    params: params,
    headers: {
      accessToken: accessToken,
    },
  });
};
export const postAx = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${url}`,
    data: params,
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      accessToken: accessToken,
    },
  });
};
export const poJson = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${url}`,
    data: params,
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      accessToken: accessToken,
    },
  });
};
export const postJson = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "post",
    url: `${url}`,
    data: params,
    headers: {
      "Content-Type": "application/json;charset=UTF-8",
      accessToken: accessToken,
    },
  });
};
export const getFrom = (url, params) => {
  let accessToken =
    getSession("accessToken") === undefined ||
    getSession("accessToken") === null
      ? ""
      : getSession("accessToken");
  return axios({
    method: "get",
    url: `${base}${url}`,
    params: params,
    transformRequest: [
      function(data) {
        let ret = "";
        for (let it in data) {
          ret +=
            encodeURIComponent(it) + "=" + encodeURIComponent(data[it]) + "&";
        }
        return ret;
      },
    ],
    headers: {
      "Content-Type": "application/x-www-form-urlencoded",
      accessToken: accessToken,
    },
  });
};
